package com.sammy.appmonitor.contentprovider;

import android.content.ContentProvider;
import android.content.ContentValues;
import android.content.Context;
import android.content.UriMatcher;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteQueryBuilder;
import android.net.Uri;
import android.text.TextUtils;

import com.sammy.appmonitor.contentprovider.MyContract.MessagesOut;
import com.sammy.appmonitor.contentprovider.MyDatabase.Tables;

public class MyProvider extends ContentProvider {

	private MyDatabase mOpenHelper;
	private static final UriMatcher sUriMatcher = buildUriMatcher();


	private static final int MESSAGESOUT = 200;
	private static final int MESSAGESOUT_MSGIN_ID = 201;
	private static final int MESSAGESOUT_ID = 202;

	private static UriMatcher buildUriMatcher() {

		final UriMatcher matcher = new UriMatcher(UriMatcher.NO_MATCH);
		final String authority = MyContract.CONTENT_AUTHORITY;

		matcher.addURI(authority, "messagesout", MESSAGESOUT);
		matcher.addURI(authority, "messagesout/msgin/*", MESSAGESOUT_MSGIN_ID);
		matcher.addURI(authority, "messagesout/*", MESSAGESOUT_ID);

		return matcher;
	}

	@Override
	public boolean onCreate() {
		final Context context = getContext();
		mOpenHelper = new MyDatabase(context);
		return true;
	}

	@Override
	public String getType(Uri uri) {
		final int match = sUriMatcher.match(uri);
		switch (match) {
		case MESSAGESOUT:
		case MESSAGESOUT_MSGIN_ID:
			return MessagesOut.CONTENT_TYPE;
		case MESSAGESOUT_ID:
			return MessagesOut.CONTENT_ITEM_TYPE;

		default:
			throw new UnsupportedOperationException("Unknown uri: " + uri);
		}
	}

	@Override
	public int delete(Uri uri, String selection, String[] selectionArgs) {
		final SQLiteDatabase db = mOpenHelper.getWritableDatabase();
		int option = sUriMatcher.match(uri);
		String table = null;
		StringBuilder where = new StringBuilder();

		switch (option) {
		case MESSAGESOUT:
			table = Tables.MESSAGES_OUT;
			break;
		default:
			// TODO
			break;
		}

		if (table != null) {

			if (!TextUtils.isEmpty(selection)) {
				where.append((where.length() == 0 ? " " : " AND ")).append(selection);
			}

			int count = db.delete(table, where.toString(), selectionArgs);

			if (count > 0) {
				getContext().getContentResolver().notifyChange(uri, null);
			}
			return count;
		} else {
			return 0;
		}

	}

	@Override
	public Uri insert(Uri uri, ContentValues values) {
		final SQLiteDatabase db = mOpenHelper.getWritableDatabase();
		int option = sUriMatcher.match(uri);
		switch (option) {
		case MESSAGESOUT:
			db.insert(Tables.MESSAGES_OUT, null, values);
			return null;

		default:
			throw new UnsupportedOperationException("Unknown uri: " + uri);
		}

	}

	@Override
	public Cursor query(Uri uri, String[] projection, String selection, String[] selectionArgs, String sortOrder) {
		SQLiteQueryBuilder queryBuilder = new SQLiteQueryBuilder();
		final SQLiteDatabase db = mOpenHelper.getWritableDatabase();
		int option = sUriMatcher.match(uri);
		switch (option) {
		case MESSAGESOUT:
			queryBuilder.setTables(Tables.MESSAGES_OUT);
			break;

		default:
			throw new UnsupportedOperationException("Unknown uri: " + uri);
		}

		Cursor cursor = queryBuilder.query(db, projection, selection, selectionArgs, null, null, sortOrder);
		cursor.setNotificationUri(getContext().getContentResolver(), uri);
		return cursor;
	}

	@Override
	public int update(Uri uri, ContentValues values, String selection, String[] selectionArgs) {
		final SQLiteDatabase db = mOpenHelper.getWritableDatabase();
		int option = sUriMatcher.match(uri);
		String table = null;
		StringBuilder where = new StringBuilder();

		switch (option) {
		case MESSAGESOUT:
			table = Tables.MESSAGES_OUT;
			break;
		default:
			// TODO
			break;
		}

		if (table != null) {
			if (!TextUtils.isEmpty(selection)) {
				if (where.length() > 0) {
					where.append(" AND ").append(selection);
				} else {
					where.append(selection);
				}
			}

			int count = db.update(table, values, where.toString(), selectionArgs);

			if (count > 0) {
				getContext().getContentResolver().notifyChange(uri, null);
			}

			return count;

		} else {
			return 0;
		}

	}

}
